function Install(num){
    for (let i = 0; i < 10; i++) {
        var res = Open_file();
        if (res) {
            return Install_APP(num);
        }
    }
}

function Open_file(){
    home();
    sleep(500);
    var file = text("FileManager");
    var result1 = waitExistNode(file, 10000);
    if (result1) {
        click(file);
        logd("进入文件系统");
        sleep(500);

        var file_management = text("本地文件管理");
        var result2 = waitExistNode(file_management, 10000);
        if (result2) {
            click(file_management);
            logd("进入本地文件管理器");
            sleep(1000);
            return true;
        }else {
            toast("没有找到本地文件管理器");
        }
    }else {
        toast("没有找到文件系统");
    }
}

function Open_download(){
    var menu = desc("Navigate up");
    var result1 = waitExistNode(menu, 10000);
    if (result1) {
        click(menu);
        logd("点击菜单...");
        sleep(500);

        var download = text("Download");
        var result2 = waitExistNode(download, 10000);
        if (result2) {
            click(download);
            logd("点击下载中心");
            sleep(1500);
            return true;
        }else {
            toast("没有找到下载中心");
        }
    }else {
        toast("没有找到菜单");
    }
}

function Install_APP(num){
    var n = 0;
    while (n<num) {
        sleep(1500);
        swipeToPoint(600, 600, 600, 1000, 300);
        sleep(1000);
        if (Open_download()) {
            logd("重新进入下载中心");
        }else {
            toast("打开下载中心失败");
        }
        var m = random(1, 20);
        var app_num = m + ".apk";
        logd("即将安装" + app_num);
        sleep(1000);
        for (let j = 0; j < 4; j++) {
            var app_name = text(app_num);
            var result1 = has(app_name);
            if (result1) {
                click(app_name);
                logd("点击安装"+app_num);
                sleep(500);

                var install1 = text("Package Installer");
                var result2 = waitExistNode(install1, 10000);
                if (result2) {
                    click(text("INSTALL"));
                    sleep(1000);

                    var install2 = text("Do you want to install this app?");
                    var result3 = has(install2);
                    if (result3) {
                        click(text("INSTALL"));
                        sleep(1000);

                        while (1){
                            var finish = text("App installed.");
                            var result5 = has(finish);
                            if (result5) {
                                click(text("DONE"));
                                logd(app_num + "完成安装");
                                n++;
                                sleep(1000);
                                break;
                            }else {
                                var wait = text("Installing…");
                                if (has(wait)) {
                                    logd("还在安装中...请稍等");
                                    sleep(2000);
                                }else {
                                    sleep(1000);
                                    var warning = text("Unsafe app blocked");
                                    if (has(warning)) {
                                        if (has(text("Got it"))) {
                                            click(text("Got it"));
                                        }else {
                                            if (has(text("OK"))) {
                                                click(text("OK"));
                                            }
                                        }
                                        sleep(1000);
                                        click(text("DONE"));
                                        sleep(1000);
                                        toast("这个APP是不安全的,重新选择");
                                        break;
                                    }
                                }
                            }
                        }
                        break;
                    }else {
                        var update = text("Do you want to update this app?");
                        if (has(update)) {
                            click(text("CANCEL"));
                            toast(app_num + "已经安装,重新选择");
                            sleep(1000);
                            break;
                        }
                    }
                }else {
                    toast("安装失败");
                }
            }else {
                swipeToPoint(500, 1800, 500, 500, 700);
                logd("没有找到"+app_num+"...继续下拉");
                sleep(1000);
            }
        }

    }
    home();
    sleep(1000);
    return true;
}

// Install(5);